<?php
require("libraries/student.php");
class Live extends Student{
    public function __construct(){
        parent::__construct();
        $this->load->model("mlive");
        $this->load->model("model_destination");
        $this->load->model("model_region");
        $this->load->model("mtype");
        $this->load->library("string");
    }
    public function index(){
        $config['base_url'] = base_url().'admin/live/index/';
        $config['total_rows'] = $this->mlive->count_all();
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $config['next_link'] = "Next";
        $config['prev_link'] = "Prev";
        $config['first_link'] = "First";
        $config['last_link'] = "Last";
        $this->load->library("pagination");
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
        $data['act'] = 0;
        $data['title']="title live";
        $data['data']['info']= $this->mlive->listall($config['per_page'],$start);
        $data['template'] = "live/list";
        $this->load->view("layout",$data);
    }
    public function region(){
        $this->load->model('model_tours');
        $id = $this->input->post("val");
        $edit = $this->input->post('edit');
        $listall = $this->model_tours->listregion($id);
        echo '<select name="region_id" id="region_id">';
        echo '<option value="0">Please select</option>';
        foreach($listall as $items){
            echo "<option value='".$items['region_id']."'"; if($edit == $items['region_id']){ echo "selected='selected'";} echo ">
            ".$items['region_title']."</option>";
        }
        echo "</select>";
    }
    public function add(){
        $this->load->helper(array("form","date"));
        $this->load->library("form_validation");
        $data['error'] = "";
        $data['data'] = "";
        $data['title'] = "title live";
        $data['template'] = "live/add";
        $data['type'] = $this->mtype->get_all();
        $data['des'] = $this->model_destination->get_all();
        $data['region'] = $this->model_region->get_all();
        if($this->input->post("ok") != ""){
            $this->form_validation->set_rules("title","title","min_length[5]|callback_check_live");
            if($this->form_validation->run() == FALSE){
                $this->load->view("layout",$data);
            }else{
                $db = array(
                    "author"=>$this->input->post("author"),
                    "nametour" =>$this->input->post("nametour"),
                    "datetour" =>$this->input->post("datetour"),
                    "type_id"=>$this->input->post('type_id'),
                    "destina_id"=>$this->input->post('destina_id'),
                    "region_id"=>$this->input->post('region_id'),
                    "country"=>$this->input->post("country"),
                    "description" => $this->input->post("description"),
                    "content" => $this->input->post("content"),
                    "status"=>1,
                    "created_time"=>time(),
                );
                $this->mlive->add($db);
                redirect(base_url().'admin/live/index');
                //redirect(base_url()."admin/live/index");
            }
        }else{
            $this->load->view("layout",$data);
        }
    }

    public function update(){
        $id = $this->uri->segment(4);
        $this->load->helper(array("form","date"));
        $this->load->library("form_validation");
        $data['error'] = "";
        $data['title'] = "title live";
        $data['template'] = "live/edit";
        $data['data'] = "";
        $data['get'] = $this->mlive->get_data($id);
        $data['type'] = $this->mtype->get_all();
        $data['des'] = $this->model_destination->get_all();
        $data['region'] = $this->model_region->get_all();
        if($this->input->post("ok") != ""){
            $this->form_validation->set_rules("title","title","min_length[5]|callback_check_live");
            if($this->form_validation->run() == FALSE){
                $this->load->view("layout",$data);
            }else{
                $db = array(
                    "author"=>$this->input->post("author"),
                    "nametour" =>$this->input->post("nametour"),
                    "datetour" =>$this->input->post("datetour"),
                    "type_id"=>$this->input->post('type_id'),
                    "destina_id"=>$this->input->post('destina_id'),
                    "region_id"=>$this->input->post('region_id'),
                    "country"=>$this->input->post("country"),
                    "description" => $this->input->post("description"),
                    "content" => $this->input->post("content"),
                    "modified_time"=>time(),
                );
                $this->mlive->update($db,$id);
                redirect(base_url().'admin/live/index');
            }
        }else{
            $this->load->view("layout",$data);
        }
    }

    public function del(){
        $id = $this->uri->segment(4);
        $this->mlive->del($id);
        redirect(base_url().'admin/live/index');
    }
    public function active(){
        $id   	= $this->uri->segment(4);
        $status	= $this->uri->segment(5);
        $db = array(
            "status" => $status
        );
        $this->mlive->update_status($db,$id);
        redirect(base_url().'admin/live/');
    }

}